;
(function($, undefined) {
	$(function() {
		$.extend($.validator.messages, {
			currency : "请输入合法的货币格式"
		});
		// ajax全局异常
		$(document).ajaxError(function(event, request, settings, error) {
			if ('ajaxError' == event.type && error instanceof SyntaxError) {
				var txt = request.responseText;
				if (txt.indexOf('<body>') > -1) {
					txt = txt.substring(txt.indexOf('<body'), txt.indexOf('</body>') + 7);
				}
				var url = $(txt).filter('#error_url').val();
				if (url) {
					location.href = url;
				}
			}
		});
	});
})(jQuery);
// 全局公共操作
(function($, undefined) {
	var _data = {
		'1' : '提交成功！',
		'2' : '操作失败，请重试！',
		'11' : '相同的数据已经存在！',
		'12' : '您提交的数据不存在！',
		'21' : '启用状态不能为空！',
		'51' : '有关联资源存在，无法删除！'
	};
	$.extend({
		jsonResult : function(data, callback) {
			if (data && ('code' in data)) {
				if (data.code == '1') {
					layer.alert(_data[data.code], {
						icon : 6,
						time : 1000
					});
					var $table = $('#table_list');
					if ($table[0]) {
						$table.triggerHandler('reload.table');
					} else {
						var $pTableList = parent.$('#table_list');
						if ($pTableList[0]) {
							$pTableList.triggerHandler('reload.table');
							setTimeout(function() {
								parent.layer.closeAll();
							}, 1000);
						}
					}
				} else if (data.code == '21' && data.msg) {
					var $form = $('form');
					for ( var name in data.msg) {
						var $el = $form.find('input[name=' + name + ']');
						$.validateTip(true, $el, data.msg[name]);
					}
				} else {
					layer.alert(_data[data.code] || '出现位置错误，请重试！', {
						icon : 5
					});
				}
				if (!!callback && $.isFunction(callback[data.code])) {
					callback[data.code](data);
				}
			}
		},
		validateTip : function(isShow, el, tips) {
			var $el = $(el);
			if (isShow) {
				var index = $el.data('layer');
				if (!!index) {
					layer.close(index);
				}
				if ($el.is(':hidden') || $el.is(':checkbox')) {
					el = $el.parent();
				}
				$el.data('layer', layer.tips(tips, el, {
					tipsMore : true,
					time : 0
				}));
			} else {
				var index = $el.data('layer');
				if (!!index) {
					layer.close(index);
				}
			}
		},
		newPage : function(title, url) {
			var index = layer.open({
				type : 2,
				title : title,
				content : url
			});
			layer.full(index);
			$('#layui-layer' + index + ' .layui-layer-close').on('click.newPage', function() {
				parent.layer.closeAll();
			});
			return false;
		}
	});
})(jQuery);